﻿#pragma once
#include"HashBucket.h"
using namespace std;
template<class K>
struct hashfunc
{
public:
	int operator()(const K& key)
	{
		return key;
	}
};

template<>
struct hashfunc<string>
	//针对于string的特化
{
public:
	size_t operator()(const string& key)
	{
		size_t hash = 0;
		for (auto e : key)
		{
			hash += e * 131;
		}
		return hash;
	}
};



template<class K,class T,class Hash=hashfunc<K>>
class MyUnordered_Map
{
public:

	template<class K, class T>
	class MapKeyofT
	{
	public:

		const K& operator()(const pair<K, T>& kv)
		{
			return kv.first;
		}
	};
	typedef typename hash_bucket::HashTable<K, pair<const K,T>, MapKeyofT<K,T>, Hash>::iterator iterator;

	iterator begin()
	{
		return _ht.begin();
	}

	iterator end()
	{
		return _ht.end();
	}

	//iterator insert(const pair<K,T>& kv)
	void insert(const pair<K,T>& kv)
	{
		//return _ht.insert(kv);
		 _ht.insert(kv);

	}
private:
	hash_bucket::HashTable<K, pair<const K,T>, MapKeyofT<K,T>, Hash> _ht;

};
void test_map1()
{
	MyUnordered_Map<string, string> dict;
	dict.insert(make_pair("sort", "排序"));
	dict.insert(make_pair("left", "左边"));
	dict.insert(make_pair("right", "右边"));

	/*auto cur = dict.begin();
	while (cur != dict.end())
	{
		cout <<cur->_value.first<<"," <<cur->_value.second<< endl;
		cur = cur->_next;
	}*/
	for (auto& kv : dict)
	{
		//kv.first += 'x';
		kv.second += 'y';

		cout << kv.first << ":" << kv.second << endl;
	}
}